from April13.pcUtils import *
import time
s=Scrawler()
for page in range(1,11):
    url='https://www.znw.com.cn/'
    '''
    #获取每一条新闻标题
    #div.news-right > div.news-header > h3 > a
    titles=s.getTargetTexts(url,selector="h3 a")
    print(titles,len(titles))
    #获取新闻链接
    newurls=s.getTargethrefs(url,selector="h3 a")
    print(newurls)
    for index,newurl in enumerate(newurls):
        contents=s.getTargetTexts(newurl,selector="div.article-main div.article-content p")
        print("新闻内容搞定")
        with open("第{}页".format(str(page))+"第{}条.txt".format(index),"w+")as f:
            f.write(str(contents))
    #获取新闻简介
    # div:nth-child(1) > div.news-right > div.news-main > p
    intro=s.getTargetTexts(url,selector="div.news-main p")
    #获取网页发布单位
    # div:nth-child(1) > div.news-right > div.news-footer > p > span.author > a
    org=s.getTargetTexts(url,selector="div.news-footer p span.author")
    #发布时间
    # div:nth-child(1) > div.news-right > div.news-footer > p > span.date
    data=s.getTargetTexts(url,selector="div.news-footer p span.date")
    with open("result.txt","a+") as f:
        f.write(r"\n 这是第{}页的新闻:\n".format(page)+str(titles))
        print("第{}页完成".format(page))
        time.sleep(1)
        '''
    #小图
    #mh_magazine_posts_digest-3 > div > div.mh-row.mh-posts-digest-wrap.mh-posts-digest-large.clearfix > article.mh-col-1-2.mh-posts-digest-item.mh-posts-digest-item-large.clearfix.post-639.post.type-post.status-publish.format-standard.has-post-thumbnail.category-china.tag-23 > figure > a > img
    #mh_magazine_posts_digest-3 > div > div.mh-row.mh-posts-digest-wrap.mh-posts-digest-large.clearfix > article.mh-col-1-2.mh-posts-digest-item.mh-posts-digest-item-large.clearfix.post-647.post.type-post.status-publish.format-standard.has-post-thumbnail.category-uav.tag-24.tag-23 > figure > a > img
    # mh_magazine_custom_posts-5 > ul > li.mh-custom-posts-item.mh-custom-posts-small.clearfix.post-685.post.type-post.status-publish.format-standard.category-focus.tag-22.tag-24 > figure > a > img
    #div.tag-list-news > div:nth-child(1) > div.news-img > a > img
    picurl=s.getImgUrl(url,selector="figure a img")
    for index,url in enumerate(picurl):
        s.getImg(url,imgName="xinwen-"+str(index)+".jpg")
        time.sleep(1)

